﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EasyCocos2D
{
    public partial class frmMain : Form
    {
        int ObjSelect;

        bool ObjIsSelect = false;
        bool IsMoveDown = false;

        Point objmove;
        Point move;

        List<CCGameObject> lstgameObj = new List<CCGameObject>();

        public frmMain()
        {
            InitializeComponent();

            penalDraw.GetType().GetMethod("SetStyle",
                  System.Reflection.BindingFlags.Instance |
                  System.Reflection.BindingFlags.NonPublic).Invoke(panel1,
                  new object[]{ System.Windows.Forms.ControlStyles.UserPaint | 
	              System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | 
	              System.Windows.Forms.ControlStyles.DoubleBuffer, true });
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            
        }

        private void penalDraw_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
                foreach (string fileLoc in filePaths)
                {
                    if (File.Exists(fileLoc))
                    {
                        CCGameObject gameobj = new CCSprite(fileLoc);

                        gameobj.Name = "Object_" + lstgameObj.Count;

                        Point p = this.PointToClient(new Point(e.X, e.Y));
                        p.X = p.X - panel1.Left;
                        p.Y = p.Y - panel1.Top;
                        gameobj.Position = p;
                        
                        lstgameObj.Add(gameobj);

                        propertyGrid1.SelectedObject = gameobj;
                        /*if (panel1.Width < (gameobj.Img.Width + gameobj.PosX))
                        {
                            panel1.Width = gameobj.Img.Width + gameobj.PosX;
                        }
                        if (panel1.Height < (gameobj.Img.Height + gameobj.PosY))
                        {
                            panel1.Height = gameobj.Img.Height + gameobj.PosY;
                        }*/
                    }
                }

                Invalidate(true);
            }
        }

        private void penalDraw_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop) == true)
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void penalDraw_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            // Vẽ các đối tượng lên màn hình
            foreach (CCGameObject gameobj in lstgameObj)
            {
                gameobj.Draw(g);
            }
        }

        private void penalDraw_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point p = new Point();
                p.X = e.X;
                p.Y = e.Y;

                int i = lstgameObj.Count - 1;

                // Kiểm tra trong danh sách đối tượng xem đối tượng nào được chọn
                foreach (CCGameObject gameobj in lstgameObj.AsEnumerable().Reverse())
                {
                    // Nếu có đối tượng được chọn
                    if (gameobj.getObject(p))
                    {
                        ObjSelect = i;
                        ObjIsSelect = true;

                        move = p;
                        objmove = gameobj.Position;

                        break;
                    }

                    i--;
                }
                IsMoveDown = true;
            }
        }

        private void penalDraw_MouseMove(object sender, MouseEventArgs e)
        {
            if (ObjIsSelect && IsMoveDown)
            {
                int x = e.X - move.X;
                int y = e.Y - move.Y;

                Point Move = new Point();
                Move.X = objmove.X + x;
                Move.Y = objmove.Y + y;

                lstgameObj[ObjSelect].Position = Move;

                Invalidate(true);
            }
        }

        private void penalDraw_MouseUp(object sender, MouseEventArgs e)
        {
            ObjIsSelect = false;
            IsMoveDown = false;
            Invalidate(true);
        }


    }
}
